﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;

/// <summary>
/// Summary description for DataProvider
/// </summary>
public class DataProvider
{

    public static string path = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source="
                         + System.Web.HttpContext.Current.Server.MapPath("~/App_Data/CSDL_XEMAY.accdb");
    public DataProvider()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    // phuong thuc Lay Du Lieu
    public static DataTable ExecuteQuery(string sql)
    {
        OleDbConnection con = new OleDbConnection();


        con.ConnectionString = path;
        con.Open();

        OleDbDataAdapter adapter = new OleDbDataAdapter(sql, con);
        DataSet ds = new DataSet();
        adapter.Fill(ds);

        con.Close();
        return ds.Tables[0];


    }

    // phuong thuc Lay Du Lieu

    public static object ExecuteScalar(string sql)
    {
        object value = null;

        OleDbConnection con = new OleDbConnection();


        con.ConnectionString = path;
        con.Open();

        OleDbCommand command = new OleDbCommand();
        command.CommandText = sql;
        command.Connection = con;

        value = command.ExecuteScalar();

        con.Close();

        return value;
    }


    public static DataTable ExecuteStructureQuery(string sql)
    {
        OleDbConnection con = new OleDbConnection();


        con.ConnectionString = path;
        con.Open();

        OleDbDataAdapter adapter = new OleDbDataAdapter(sql, con);
        DataSet ds = new DataSet();
        adapter.FillSchema(ds,SchemaType.Source);

        con.Close();
        return ds.Tables[0];


    }


    // phuong thuc Cap nhat thay doi tren Du lieu
    // them , xoa, sua
    public static void ExecuteNonQuery(string sql)
    {
        OleDbConnection con = new OleDbConnection();


        con.ConnectionString = path;
        con.Open();

        OleDbCommand command = new OleDbCommand();
        command.CommandText = sql;
        command.Connection = con;

        command.ExecuteNonQuery();

        con.Close();
    }

    public static bool Save(DataRow Dong,String Ten_Bang)
    {
        DataTable Bang = Dong.Table;
        String sql = "Select * from " + Ten_Bang;
        OleDbDataAdapter BPS = new OleDbDataAdapter(sql, path);
        OleDbCommandBuilder BXD = new OleDbCommandBuilder(BPS);
        if (BPS.Update(Bang) > 0)
        { return true; }
        else { return false; }

    }
    public static bool Delete(DataRow Dong, String Ten_Bang)
    {
        DataTable Bang = Dong.Table;
        Dong.Delete();
        String sql = "Select * from " + Ten_Bang;
        OleDbDataAdapter BPS = new OleDbDataAdapter(sql, path);
        OleDbCommandBuilder BXD = new OleDbCommandBuilder(BPS);
        BPS.Update(Bang);
        if (BPS.Update(Bang) > 0)
        { return true; }
        else { return false; }
    }

}

